/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.fachadas;

import edu.unicauca.opentextbook.entidades.Rol;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
import javax.persistence.Query;
import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Gamificaion
 */
@Stateless
public class RolFacade extends AbstractFacade<Rol> {

    @PersistenceContext(unitName = "openTextBookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public RolFacade() {
        super(Rol.class);
    }

    public Rol buscarRol(int idRol) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        Rol rol = null;

        sql.append("SELECT * ");
        sql.append("FROM rol r ");
        sql.append("WHERE r.idRol = ? ");

        try {
            query = em.createNativeQuery(sql.toString(), Rol.class);
            query.setParameter(1, idRol);

            rol = (Rol) query.getSingleResult();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_NoResultException:".concat(e.getMessage()));
        } catch (NonUniqueResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_NonUniqueResultException:".concat(e.getMessage()));
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_IllegalStateException:".concat(e.getMessage()));
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_QueryTimeoutException:".concat(e.getMessage()));
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_TransactionRequiredException:".concat(e.getMessage()));
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_PersistenceException:".concat(e.getMessage()));
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarRol_Exception:".concat(e.getMessage()));
        } finally {
            return rol;
        }
    }

    public List<Rol> buscarTodos() {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Rol> litaRoles = null;

        sql.append("SELECT * ");
        sql.append("FROM rol");

        try {
            query = em.createNativeQuery(sql.toString(), Rol.class);

            litaRoles = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_NoResultException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_IllegalStateException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_QueryTimeoutException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_TransactionRequiredException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_PessimisticLockException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_LockTimeoutException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_PersistenceException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_Exception:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } finally {
            return litaRoles;
        }
    }

    public List<Rol> buscarTodosMenosSenior() {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Rol> litaRoles = null;

        sql.append("SELECT * ");
        sql.append("FROM rol ");
        sql.append("WHERE rol.idRol != 1");

        try {
            query = em.createNativeQuery(sql.toString(), Rol.class);

            litaRoles = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_NoResultException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_IllegalStateException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_QueryTimeoutException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_TransactionRequiredException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_PessimisticLockException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_LockTimeoutException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_PersistenceException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodosMenosSenior_Exception:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } finally {
            return litaRoles;
        }
    }

    public List<Rol> consultarRolesUsuarioPorGrupo(int idUsuario, int idGrupo) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Rol> litaRoles = null;

        sql.append("SELECT r.* ");
        sql.append("FROM pertenece p ");
        sql.append("INNER JOIN rol r ON p.idRol = r.idRol ");
        sql.append("WHERE p.idUsuario = ? ");
        sql.append("AND p.idGrupo = ?");

        try {
            query = em.createNativeQuery(sql.toString(), Rol.class);
            query.setParameter(1, idUsuario);
            query.setParameter(2, idGrupo);

            litaRoles = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_NoResultException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_IllegalStateException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_QueryTimeoutException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_TransactionRequiredException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_PessimisticLockException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_LockTimeoutException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_PersistenceException:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarRolesUsuarioPorGrupo_Exception:".concat(e.getMessage()));
            litaRoles = new ArrayList<Rol>();
        } finally {
            return litaRoles;
        }
    }
}
